﻿using System.Collections.ObjectModel;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using WpfApp60.Models;

namespace WpfApp60
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private ListCollectionView view = null!;
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            ObservableCollection<Product> products = new ObservableCollection<Product>();
            for (int i = 0; i < 10; i++)
            {
                products.Add(new Product()
                {
                    Id = i,
                    Name = $"Product {i}",
                    UnitCost = 1 + i,
                    Description = $"Description :{i} "
                });
            }

            this.DataContext = products;

            view = (ListCollectionView)CollectionViewSource.GetDefaultView(this.DataContext);
            view.CurrentChanged += View_CurrentChanged;

            string info = $"Record {view.CurrentPosition} of {view.Count}";
            TbInfo.Text = info;
        }

        private void View_CurrentChanged(object? sender, EventArgs e)
        {
            string info = $"Record {view.CurrentPosition} of {view.Count}";
            TbInfo.Text = info;

            //当数据到了最后一条的时候，需要是下一条按钮禁用
            BtnNext.IsEnabled = view.CurrentPosition < view.Count - 1;
            BtnPre.IsEnabled = view.CurrentPosition > 0;
        }

        private void BtnPre_Click(object sender, RoutedEventArgs e)
        {
            view.MoveCurrentToPrevious();
        }

        private void BtnNext_Click(object sender, RoutedEventArgs e)
        {
            //切换到下一条数据
            view.MoveCurrentToNext();
        }
    }
}